package com.order.service.impl;

import com.order.dao.OrderDao;
import com.order.domain.Order;
import com.order.service.AccountService;
import com.order.service.OrderService;
import com.order.service.StockService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private AccountService accountService;
    @Autowired
    private StockService stockService;


    @Override
    @GlobalTransactional(name = "order_tx_group",rollbackFor = Exception.class)
    public int createOrder(Order order) {

        //1、生成订单
        int result = orderDao.createOrder(order);
        //2、扣减账户余额
        accountService.pay(order.getUserId(),order.getMoney());
        //3、减库存
        stockService.reduction(order.getProductId(),order.getCount());

        return result;
    }
}